package v1.chapter12.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

public class MenuFrame extends JFrame {
	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;
	
	private Action saveAction;
	private Action saveAsAction;
	private JCheckBoxMenuItem readonlyItem;
	private JPopupMenu popup;
	
	class TestAction extends AbstractAction{ 
		public TestAction(String name) {
			super(name);
		}
		
		public void actionPerformed(ActionEvent e) { 
			System.out.println(getValue(Action.NAME)+" selected.");
		} 
	}
	
	public MenuFrame() {
		setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
		
		JMenu fileMenu=new JMenu("File");
		fileMenu.add(new TestAction("New"));
		
		JMenuItem openItem=fileMenu.add(new TestAction("Open"));
		openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));
		
		fileMenu.addSeparator();
		
		saveAction=new TestAction("Save");
		JMenuItem saveItem=fileMenu.add(saveAction);
		saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
		
		saveAsAction=new TestAction("Save As");
		fileMenu.add(saveAsAction);
		fileMenu.addSeparator();
		
		fileMenu.add(new AbstractAction("Exit") { 
			@Override
			public void actionPerformed(ActionEvent e) { 
				System.exit(0);
			} 
		});
		
		readonlyItem =new JCheckBoxMenuItem("Read-only");
		readonlyItem.addActionListener(new ActionListener() { 
			@Override
			public void actionPerformed(ActionEvent e) {
				boolean saveOk=!readonlyItem.isSelected(); 
				saveAction.setEnabled(saveOk);
				saveAsAction.setEnabled(saveOk);
			} 
		});
		
		ButtonGroup group =new ButtonGroup();
		JRadioButtonMenuItem insertItem=new JRadioButtonMenuItem("Insert");
		insertItem.setSelected(true);
		
		JRadioButtonMenuItem overtypeItem=new JRadioButtonMenuItem("Overtype");
		group.add(insertItem);
		group.add(overtypeItem);
		
		Action cutAction=new TestAction("Cut");
		cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif")); 
		Action copyAction=new TestAction("Copy");
		copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
	    Action pasteAction = new TestAction("Paste");
	    pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));
	    
	    JMenu editMenu=new JMenu("Eidt");
	    editMenu.add(cutAction);
	    editMenu.add(copyAction);
	    editMenu.add(pasteAction);
	    
	    JMenu optionMenu=new JMenu("Options");
	    optionMenu.add(readonlyItem);
	    optionMenu.addSeparator();
	    optionMenu.add(insertItem);
	    optionMenu.add(overtypeItem);
	     
	    editMenu.addSeparator();
	    editMenu.add(optionMenu);
	    
	    JMenu helpMenu=new JMenu("Help");
	    helpMenu.setMnemonic('H');
	    
	    JMenuItem indexItem=new JMenuItem("Index");
	    indexItem.setMnemonic('I');
	    helpMenu.add(indexItem);
	    
	    Action aboutAction =new TestAction("About");
	    aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
	    helpMenu.add(aboutAction);
	    
	    JMenuBar menuBar=new JMenuBar();
	    setJMenuBar(menuBar);
	    
	    menuBar.add(fileMenu);
	    menuBar.add(editMenu);
	    menuBar.add(helpMenu);
	    
	    popup=new JPopupMenu();
	    popup.add(cutAction);
	    popup.add(copyAction);
	    popup.add(pasteAction);
	    
	    JPanel panel=new JPanel();
	    panel.setComponentPopupMenu(popup);
	    add(panel);
	}
}
